package org.jeecg.modules.golf.question.query.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.jeecg.modules.golf.question.client.QuestionPage;
import org.jeecg.modules.golf.question.client.qto.QuestionQueryPageDTO;
import org.jeecg.modules.golf.question.entity.QuestionTypeMapping;
import org.jeecg.modules.golf.question.mapper.QuestionMapper;
import org.jeecg.modules.golf.question.query.QuestionQueryService;
import org.jeecg.modules.golf.question.service.IQuestionService;
import org.jeecg.modules.golf.question.service.IQuestionTypeMappingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author jiguansheng
 * @date 2020/10/26
 **/
@Service
public class QuestionQueryServiceImpl implements QuestionQueryService {


    @Autowired
    private IQuestionService questionService;

    @Autowired
    private QuestionMapper questionMapper;

    @Autowired
    private IQuestionTypeMappingService questionTypeMappingService;
    /**
     * 问题列表分页
     *
     * @param questionQueryPageDTO
     * @return
     */
    @Override
    public IPage<QuestionPage> page(QuestionQueryPageDTO questionQueryPageDTO) {
        List<Long> questionIdList = null;
        if(questionQueryPageDTO.getQuestionTypeId() != null){
            QuestionTypeMapping questionTypeMapping = new QuestionTypeMapping();
            questionTypeMapping.setQuestionTypeId(questionQueryPageDTO.getQuestionTypeId());
            QueryWrapper<QuestionTypeMapping> QuestionTypeMappingQueryWrapper = new QueryWrapper<>(questionTypeMapping);
            List<QuestionTypeMapping> QuestionTypeMappingList =questionTypeMappingService.list(QuestionTypeMappingQueryWrapper);
            questionIdList = QuestionTypeMappingList.stream().map(QuestionTypeMapping::getMappingId).collect(Collectors.toList());
            if(questionIdList.isEmpty()){
                return new Page();
            }
            questionQueryPageDTO.setIdList(questionIdList);
        }
        IPage<QuestionPage> questionPage = questionMapper.page(questionQueryPageDTO);
        if(questionPage.getTotal() ==0){
            return new Page<>();
        }
        List<QuestionPage> questionList = questionPage.getRecords();
        Map<Long,List<QuestionTypeMapping>> questionTypeMap = queryQuestionType(questionList);
        for(QuestionPage question  :questionList){
            List<QuestionTypeMapping> questionTypeMappings = questionTypeMap.get(question.getId());
            if(questionTypeMappings != null){
                question.setQuestionTypeJoin(questionTypeMappings.stream().map(QuestionTypeMapping::getQuestionTypeName).collect(Collectors.joining(",")));
            }
        }
        return questionPage;
    }

    /**
     * 查询方案的方案分类集合
     * @param questionList
     * @return
     */
    private Map<Long,List<QuestionTypeMapping>> queryQuestionType(List<QuestionPage> questionList){
        List<Long> questionIdList =   questionList.stream().map(QuestionPage::getId).collect(Collectors.toList());
        if(questionIdList.size() == 0) {
            return new HashMap<>();
        }
        List<QuestionTypeMapping> QuestionTypeMappingList = questionTypeMappingService.selectByMappingIdList(questionIdList, 1);
        if(CollectionUtil.isEmpty(QuestionTypeMappingList)){
            return new HashMap<>();
        }
        return   QuestionTypeMappingList.stream().collect(Collectors.groupingBy(QuestionTypeMapping::getMappingId));
    }

}
